package com.wnhz.work.yz.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.wnhz.work.yz.R;

/**
 * Created by admin on 2017/3/9.
 */
public class MyRoundView extends View {
    public MyRoundView(Context context) {
        this(context, null);
    }

    public MyRoundView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private int mLifeCostColor, trafficCostColor, mCommunicationCostColor, mCircleWidth, mRadius;

    public MyRoundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundRateView, defStyleAttr, 0);
        int n = array.getIndexCount();//自定义属性个数
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);//通过索引获取具体属性
            switch (attr) {
                case R.styleable.RoundRateView_lifeCostColor:
                    mLifeCostColor = array.getColor(attr, Color.parseColor("#fac62d"));
                    break;
                case R.styleable.RoundRateView_trafficCostColor:
                    trafficCostColor = array.getColor(attr, Color.parseColor("#65cff6"));
                    break;
                case R.styleable.RoundRateView_communicateCostColor:
                    mCommunicationCostColor = array.getColor(attr, Color.parseColor("#fe9a9c"));
                    break;
                case R.styleable.RoundRateView_entertainmentCostColor:
                    mCommunicationCostColor = array.getColor(attr, Color.parseColor("#a286da"));
                    break;
                case R.styleable.RoundRateView_circleWidth:
                    mCircleWidth = array.getDimensionPixelSize(attr, dp2px(20));
                    break;
                case R.styleable.RoundRateView_radius:
                    mRadius = array.getDimensionPixelSize(attr, dp2px(20));
                    break;
            }
        }
        array.recycle();//定义完后属性对象回收</span>
        initPaint();
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mWidth = widthMeasureSpec;
        int mHeight = heightMeasureSpec;
        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec); //获取控件中layout_width设定大小模式（有wrap_content,match_parent和具体数值）
        int specSize = MeasureSpec.getSize(widthMeasureSpec); //获取系统设定的控件宽度

        if (specMode == MeasureSpec.EXACTLY)//当控件大小设定为match_parent或者是具体值时
        {
            Log.e("xxx", "EXACTLY");
            mWidth = specSize;
        } else {

            if (specMode == MeasureSpec.AT_MOST)//控件layout_width设定为wrap_content，将控件宽度设置为圆环半径的两倍
            {
                mWidth = (int) (mRadius * 2);
                Log.e("xxx", "AT_MOST");
            }
        }

        /***
         * 设置高度
         */
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mHeight = specSize;
        } else {
            if (specMode == MeasureSpec.AT_MOST)//控件layou_hight设定为wrap_content，将控件高度设置为圆环半径的两倍
            {
                mHeight = (int) (mRadius * 2);
            }
        }
        setMeasuredDimension(mWidth + 10, mHeight + 10);//最后应用到项目的时候发现圆饼有些超出控件定义的大小，所以将控件总大小稍微加大一点
    }

    Paint mPaint;

    //初始化画笔
    private void initPaint() {
        mPaint = new Paint();  //创建画笔
        mPaint.setAntiAlias(true);  //设置绘制时抗锯齿
        mPaint.setStyle(Paint.Style.STROKE); //设置绘画空心（比如画圆时画的是空心圆而不是实心圆）
    }

    RectF mRectF;

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setStrokeWidth(mCircleWidth);//设置画笔线宽
        //创建矩形对象，关于RectF，androidAPI中有如下说明
        /**
         * Create a new rectangle with the specified coordinates. Note: no range
         * checking is performed, so the caller must ensure that left <= right and
         * top <= bottom.
         * public RectF(float left, float top, float right, float bottom)//这是我加进去的，本人英文水平有限，但大概知道它的意思是“根据指定的坐标点创建矩形对象，还有
         * 什么左边距值不能大于右边距值，上边距值不能大于下边距值不拉不拉不拉。。。”看了下面绘制圆环的代码之后大致知道这是干什么用的（应该是限定圆环所在位置吧），至
         * 于它底层究竟是什么原理，我没去深究，反正拿来主义吧<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />（话说就是这个东西，久久不能确定圆弧的圆点，花了好长一段时间试验了好久才找到圆点画出圆弧（泪崩））
         *
         * @param left   The X coordinate of the left side of the rectangle
         * @param top    The Y coordinate of the top of the rectangle
         * @param right  The X coordinate of the right side of the rectangle
         * @param bottom The Y coordinate of the bottom of the rectangle
         */
        mRectF = new RectF(mCircleWidth / 2, mCircleWidth / 2, mRadius * 2 - mCircleWidth / 2, mRadius * 2 - mCircleWidth / 2);//其实就是个绘制圆弧的规则
        float startAngle = -90;//经过试验，-90这个值就是12点方向的位置
        float lifeSweepAngle = 150;//圆弧弧度
        mPaint.setColor(mLifeCostColor);//设置画笔颜色
        canvas.drawArc(mRectF, startAngle, lifeSweepAngle, false, mPaint);//这里就是真正绘制圆弧的地方，从12点方向开始顺时针绘制150度弧度的圆弧
        //以下无需我来解释...
        startAngle = startAngle + lifeSweepAngle;
        float communicateSweep = 50;
        mPaint.setColor(mCommunicationCostColor);
        canvas.drawArc(mRectF, startAngle, communicateSweep, false, mPaint);

        startAngle = startAngle + communicateSweep;
        float trafficSweep = 95;
        mPaint.setColor(trafficCostColor);
        canvas.drawArc(mRectF, startAngle, trafficSweep, false, mPaint);

        startAngle = startAngle + trafficSweep;
        float entertainmentSweep = 65;
        mPaint.setColor(Color.RED);
        canvas.drawArc(mRectF, startAngle, entertainmentSweep, false, mPaint);

    }
}
